import {get} from '~/plugins/axios';

export const state = () => ({
  department: [],
});

function treefilter (arrayTree) {
  if (typeof arrayTree === 'undefined') return undefined;
  let tree = [];
  arrayTree.forEach(item => {
    if (item.children && item.children.length === 0) {
      delete item.children;
    } else {
      item.children = treefilter(item.children);
    }
    delete item.disabled;
    tree.push(item);
  });
  return tree;
}

export const mutations = {
  SET: function (state, payload) {
    state.department = payload;
  },
};

export const actions = {
  USE: async function ({state, commit}) {
    if (state.department.length === 0) {
      commit('SET', treefilter(await get('/dictionaries/getDepartmentTree')));
    }
  },
};
